package com.webull.information.center.carwler.common.util.news;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.webull.information.center.carwler.common.util.jsoup.HttpJsoupClient;

/**
 * 路透个股新闻列表
 * 
 * @author shimingjun
 * @date 2016年8月30日 下午1:42:50
 * @version 1.0
 * @since JDK 1.8
 */
@Component
public class ReutersNewsListUitl {
	private @Autowired
    HttpJsoupClient htmlPrase;
	protected final Logger logger = LogManager.getLogger(getClass());

	/**
	 * 获取新浪个股资讯首页对象并解析新闻条目列表
	 * 
	 * @param url
	 * @return
	 * @since 0.1.0
	 */
	public List<ReutersItem> parseNewsList(String url) {

		Document doc = htmlPrase.parseDocumentFromUrl(url, true);
		if (doc == null) {
			logger.warn("ReutersNewsListUitl parseNewsList parse url:{} ,doc is null!!", url);
			return null;
		}
		return parseNewsList(doc);
	}

	private List<ReutersItem> parseNewsList(Document doc) {
		List<ReutersItem> result = new ArrayList<ReutersItem>();

		final Consumer<Element> operation = it0 -> {
			ReutersItem item = new ReutersItem();
			Optional.ofNullable(it0.select("h2>a").first()).ifPresent(a0 -> {
				item.setLink(StringUtils.stripToNull(a0.absUrl("href")));
				item.setTitle(StringUtils.stripToNull(a0.html()));
			});
			Optional.ofNullable(it0.select("p").first()).ifPresent(p0 -> {
				item.setSummary(StringUtils.stripToNull(p0.ownText()));
			});

			if (StringUtils.isNotBlank(item.getLink()) && StringUtils.isNotBlank(item.getTitle()) //
					&& !StringUtils.startsWithIgnoreCase(item.getTitle(), "UPDATE "))// update
																						// 标题的新闻忽略
			{
				result.add(item);
			}
		};

		// 美股新闻页面
		Optional.ofNullable(doc.select("div.module div.moduleBody div.topStory").first()).ifPresent(topStory -> {
			operation.accept(topStory);
		});

		Optional.ofNullable(doc.select("div.module div.moduleBody div.feature")).ifPresent(features -> {
			for (Element feature : features) {
				operation.accept(feature);
			}
		});
		return result;
	}

	public static void main(String[] args) throws IOException {

		String url2 = "http://www.reuters.com/finance/stocks/companyNews?symbol=AAPL.O";
		// url2 =
		// "http://www.reuters.com/finance/stocks/companyNews?symbol=600036.SS";

		Connection connection = Jsoup.connect(url2).userAgent(
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36")
				// .header("x-client-data",
				// "CIq2yQEIpbbJAQjEtskBCP2VygEIwpjKAQjwnMoB")
				.header("x-client-data", RandomStringUtils.randomAlphanumeric(40));
		connection.proxy("127.0.0.1", 1080);
		org.jsoup.nodes.Document doc = connection.timeout(10000).get();
		List<ReutersItem> items = new ReutersNewsListUitl().parseNewsList(doc);
		System.out.println(items);
	}

	public static class ReutersItem {

		private String title;
		private String link;
		private String summary;

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getLink() {
			return link;
		}

		public void setLink(String link) {
			this.link = link;
		}

		public String getSummary() {
			return summary;
		}

		public void setSummary(String summary) {
			this.summary = summary;
		}

	}

}
